//
//  UserHomeController.m
//  GuShiNews
//
//  Created by huachun on 2019/8/21.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "UserHomeController.h"
#import "UserCenterHeaderView.h"
#import "HomeLunTVModel.h"
#import "LunTanModel.h"
#import "HomeCell.h"
#import "HomeAction.h"
#import "UserJubuViewModel.h"
#import "HUserIInfoViewModel.h"
#import "AddBlackViewModel.h"
#import "AddFollowViewModel.h"
#import "HiChatInfoController.h"
#import "RootSuperAction.h"

@interface UserHomeController ()<GetHomeLunDelegate,HUserIInfoDelegate,AddBlackDelegate,AddFollowDelegate>{
    NSInteger page;
    BOOL black_user;
    BOOL toblack_user;
    BOOL follow_user;
    BOOL feisi_user;

    BOOL Chat_ok;
}

@end

@implementation UserHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = _Model.name;
    
    self.Data_array = [NSMutableArray new];
    
    [self.view addSubview:self.TableView];
    self.TableView.frame=CGRectMake(0, 0, RECRWITCH, RECERHEIGHT);
//    self.TableView.backgroundColor = backColor;
    [self.TableView registerClass:[UserCenterHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    MJHiCarHeader *header           = [MJHiCarHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadnew)];
    header.automaticallyChangeAlpha    = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden           = YES;
    self.TableView.mj_header          = header;
    self.TableView.mj_header.automaticallyChangeAlpha = YES;
    self.TableView.mj_footer           = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self laodmore];
    }];
    self.TableView.mj_footer.hidden    = YES;
    [self.TableView.mj_header beginRefreshing];
    
    
    if ([UserModel info].userid) {
        if (![[UserModel info].userid isEqualToString:_Model.userid]) {
            NSMutableDictionary *lindic = [NSMutableDictionary new];
            [lindic setValue:[UserModel info].userid forKey:@"userid"];
            [lindic setValue:_Model.userid forKey:@"touserid"];
            HUserIInfoViewModel *viewmodel = [[HUserIInfoViewModel alloc] init:lindic];
            viewmodel.delegate=self;
        }
    }else{
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[JonesButtonItem PublicBarButtonItemPian], [JonesButtonItem NavImgButton:[UIImage imageNamed:@"news_more"] andaction:@selector(jubu_nouser) andtarget:self], nil];
    }
}

- (void)jubu_nouser{
    [UserJubuViewModel show:self dic:@{@"type":@"1",@"to_userid":@"0",@"to_id":@"3"}];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.TableView reloadData];
}

- (void)Succes:(int)value dic:(NSDictionary *)dic{
    if (value==1) {
        black_user = [[dic objectForKey:@"HeiMei"] boolValue];
        follow_user = [[dic objectForKey:@"Guanzhu"] boolValue];
        feisi_user = [[dic objectForKey:@"Fensi"] boolValue];
        toblack_user = [[dic objectForKey:@"ToHeiMei"] boolValue];
        
        if (toblack_user) {
            [SVProgressHUD showErrorWithStatus:@"对不起,对方已将您拉入黑名单!"];
            [self goback];
        }
        
        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:[JonesButtonItem PublicBarButtonItemPian], [JonesButtonItem NavImgButton:[UIImage imageNamed:@"news_more"] andaction:@selector(fabu) andtarget:self], nil];
    
        [self.TableView reloadData];
    }
}

- (void)fabu{
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [UserJubuViewModel show:self dic:@{@"type":@"1",@"to_userid":@"0",@"to_id":@"3"}];
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:black_user?@"移除黑名单":@"加入黑名单" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self post_black];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];

}

- (void)post_black{
    NSMutableDictionary *lindic = [NSMutableDictionary new];
    [lindic setValue:[UserModel info].userid forKey:@"userid"];
    [lindic setValue:_Model.userid forKey:@"touserid"];
    if (!black_user) {
        [lindic setValue:@"0" forKey:@"type"];
    }else{
        [lindic setValue:@"1" forKey:@"type"];
    }
    AddBlackViewModel *viewmodel = [[AddBlackViewModel alloc] init:lindic];
    viewmodel.delegate=self;
}

-(void)AddBlackSucces:(int)value{
    if (value==1) {
        [SVProgressHUD showSuccessWithStatus:@"操作成功"];
        black_user = !black_user;
    }
}

- (void)loadnew{
    page = 0;
    self.TableView.mj_footer.hidden    = YES;
    [self laod_net_dic];
}

- (void)laodmore{
    LunTanModel *modellin = [self.Data_array lastObject];
    page = [modellin.lun_id integerValue];
    [self laod_net_dic];
}

- (void)laod_net_dic{
    NSMutableDictionary *postdic = [NSMutableDictionary new];
    [postdic setValue:[NSString stringWithFormat:@"%li",(long)page] forKey:@"page"];
    [postdic setValue:_Model.userid forKey:@"user_id"];
    [postdic setValue:@"10" forKey:@"type"];
    HomeLunTVModel *viewmodel = [[HomeLunTVModel alloc] init:postdic];
    viewmodel.delegate = self;
}

-(void)GetHomeLunSucces:(int)value array:(NSMutableArray *)array page:(NSInteger)page{
    if (value == 1) {
        if (page==0) {
            [self.Data_array removeAllObjects];
        }
        [self.Data_array addObjectsFromArray:array];
        
        if (array.count<15) {
            self.TableView.mj_footer.hidden = YES;
        }else{
            self.TableView.mj_footer.hidden = NO;
        }
    }
    [self.TableView.mj_header endRefreshing];
    [self.TableView.mj_footer endRefreshing];
    [self.TableView reloadData];
}

- (void)gunahu{
    NSMutableDictionary *lindic = [NSMutableDictionary new];
    [lindic setValue:[UserModel info].userid forKey:@"user_id"];
    [lindic setValue:_Model.userid forKey:@"touserid"];
    if (!follow_user) {
        [lindic setValue:@"0" forKey:@"type"];
    }else{
        [lindic setValue:@"1" forKey:@"type"];
    }
    AddFollowViewModel *viewmodel = [[AddFollowViewModel alloc] init:lindic];
    viewmodel.delegate=self;
}

- (void)addFollowSucces:(int)value{
    if (value==1) {
        follow_user = !follow_user;
        [self.TableView reloadData];
    }
}

- (void)gotochat{
    HiChatInfoController *chatvc = [HiChatInfoController new];
    chatvc.hidesBottomBarWhenPushed = YES;
    chatvc.Model = _Model;
    [self.navigationController pushViewController:chatvc animated:YES];
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UserCenterHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    view.Model = _Model;
    view.slecktblock = ^(NSInteger type) {
        
        if (![UserModel info].userid) {
            [RootSuperAction Actiontodone:JonesGOTO_Login view:self dictionary:@{}];
        }else{
            if (type==0) {
                [self gunahu];
            }else{
                [self gotochat];
            }
        }
    };
    view.backgroundColor = [UIColor whiteColor];
    if (follow_user) {
        [view.Guanzhu setTitle:@"已关注" forState:UIControlStateNormal];
    }else{
        [view.Guanzhu setTitle:@"关注TA" forState:UIControlStateNormal];
    }
    view.Guanzhu.userInteractionEnabled = YES;
    view.Sixin.userInteractionEnabled = YES;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ([UserModel info].userid) {
        if ([[UserModel info].userid isEqualToString:_Model.userid]) {
            return 120;
        }
    }
    return 200;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.Data_array.count;
}

#pragma mark - UITableView DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeCell *cell = [HomeCell cellForTableView:tableView cellForRowAtIndexPath:indexPath];
    cell.Model = [self.Data_array objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark - UITableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LunTanModel *model = [self.Data_array objectAtIndex:indexPath.row];
    
    if (model.Img_array.count>0) {
        return 160+(RECRWITCH-40)/3 + 10;
        
    }
    
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LunTanModel *model = [self.Data_array objectAtIndex:indexPath.row];
    [HomeAction Actiontodone:JonesGOTO_Home_Info view:self dictionary:@{@"model":model}];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
